package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.StoreContractDto;
import com.dingreading.cloud.admin.entity.StoreContract;
import com.dingreading.cloud.admin.entity.table.StoreContractTableDef;
import com.dingreading.cloud.admin.mapper.StoreContractMapper;
import com.dingreading.cloud.admin.service.StoreContractService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.CorporationsTableDef.corporations;
import static com.dingreading.cloud.admin.entity.table.StoresTableDef.stores;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-11-19
 */
@Service
public class StoreContractServiceImpl extends BaseServiceImpl<StoreContractMapper, StoreContract, StoreContractTableDef> implements StoreContractService {

    @Override
    protected StoreContractTableDef getTable() {
        return StoreContractTableDef.storeContract;
    }

    @Override
    public Page<StoreContractDto> pageList(
            PageUtil pageUtil, String keyword, Integer depositStatus, Integer depositRefundStatus,
            Integer contractType, String beginDate, String endDate
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.contractCode.like(keyword)
                    .or(table.amount.like(keyword))
                    .or(table.deposit.like(keyword))
                    .or(table.sellerName.like(keyword))
                    .or(stores.storeName.like(keyword))
                    .or(corporations.corpName.like(keyword))
            );
        }
        if (depositStatus != null) {
            if (depositStatus == 0)
                condition.and(table.deposit.eq(0).or(table.deposit.isNull()));
            else
                condition.and(table.deposit.gt(0));
        }
        if (depositRefundStatus != null) {
            condition.and(table.depositRefundStatus.eq(depositRefundStatus));
        }
        if (contractType != null) {
            condition.and(table.contractType.eq(contractType));
        }
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            condition.and(table.endDate.between(beginDate, endDate));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, stores.storeName, corporations.corpName)
                .from(table)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .leftJoin(corporations).on(table.corpUid.eq(corporations.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateWithRelationsAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, StoreContractDto.class);
    }

    @Override
    public boolean updateSignStatus(Long id, Integer signStatus) {
        return UpdateChain.of(StoreContract.class)
                .set(table.signStatus, signStatus)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<StoreContract> listByStoreUid(String storeUid) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid));
    }

}
